package com.zsqx.back.action;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;


import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.zsqx.action.BaseAction;
import com.zsqx.model.School;
import com.zsqx.model.Student;
import com.zsqx.tools.ExcelTools;
import com.zsqx.tools.PageBean;
import com.zsqx.tools.PropertiesTools;

@Action(value="/student",results={
		@Result(name="logout",location="/"),
		@Result(name="logon",location="/main.xhtml",type="redirect"),
		@Result(name="stuDetail",location="/back/logon/student/stuDetail.jsp"),
		@Result(name="stuList",location="/back/logon/student/stuList.jsp"),
		@Result(name="outputExcel",type="stream",params={"contentType","application/vnd.ms-excel","bufferSize","2046","inputName","excelStream","contentDisposition","attachment;filename=${fileNames}"}),
		@Result(name="input",location="/500.jsp")
})
public class StudentAction extends BaseAction {
	
	private Student student;
	private List stuList;
	private List schools;
	private String operation;//0-跳转到添加页面,1-查询单个,2-跳转到修改单个,3-批量删除,4-删除单个
	private InputStream excelStream;
	private String fileNames;
	
	public List getSchools() {
		return baseDAO.findAll(new School());
	}

	/**
	 * 登录
	 * @return
	 */
	public String login(){
		student=(Student) baseDAO.findOne(student);
		if(null!=student){
			session.put("user", student);
			return "logon";
		}else{
			return "logout";
		}
	}
	
	/**
	 * 分页列表
	 * @return
	 */
	public String list(){
		if(null==student){
			student =new Student();
		}
		Criteria criteria=baseDAO.getSession().createCriteria(Student.class);
		pageBean = baseDAO.getPageBean(currentPage, baseDAO.getCount(criteria), PageBean.PAGE_SIZE);
		stuList=baseDAO.findByPage(student, pageBean, null);
		return "stuList";
	}
	
	
	/**
	 * 跳转详细
	 * @return
	 */
	public String detail(){
		if(null==operation||"".equals(operation)){
			operation="1";
		}
		if(operation.equals("1")||operation.equals("2")){
			student=(Student)baseDAO.findById(student);
			return "stuDetail";
		}else if(operation.equals("4")){
			baseDAO.delete(student);
			message=SUCCESS;
			return list();
		}else if(operation.equals("3")){
			String ids=request.getParameter("ids");
			baseDAO.deleteByBatch(new Student(),ids);
			message=SUCCESS;
			return list();
		}else if(operation.equals("0")){
			return "stuDetail";
		}else{
			return list();
		}
	}
	
	/**
	 * 执行修改或者保存
	 * @return
	 */
	public String merge(){
		if(null!=student){
			if(null==student.getCreatetime()){
				student.setCreatetime(new Date());
			}
			student.setModifytime(new Date());
		}
		try {
			baseDAO.merge(student);
			student=null;
		} catch (Exception e) {
			e.printStackTrace();
		}
		message=SUCCESS;
		return list();
	}

	/**
	 * 导出excel
	 * @return
	 */
	public String outputExcel(){
		return "outputExcel";
	}
	
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}

	public List<Student> getStuList() {
		return stuList;
	}

	public void setStuList(List<Student> stuList) {
		this.stuList = stuList;
	}

	public String getOperation() {
		return operation;
	}

	public void setOperation(String operation) {
		this.operation = operation;
	}

	public InputStream getExcelStream() {
		String absolute_path=request.getRealPath("/");
		Map<String, String> map=new HashMap<String, String>();
		map.put("id", "编号");
		map.put("name", "用户名");
		map.put("pwd", "密码");
		map.put("createtime", "创建时间");
		map.put("modifytime", "最后修改时间");
		List<Student> excelList=baseDAO.findAll(new Student());
		InputStream in=null;
		if (excelList.size()>0) {
			try {
				fileNames=ExcelTools.writeToExcel("学生列表", absolute_path,excelList, map);
				String realPath=(PropertiesTools.getExcelUploadURL()+fileNames).replace('\\', '/');
				in=ServletActionContext.getServletContext().getResourceAsStream(realPath);
				if(null==in){
					System.out.println("Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the <param name=\"inputName\"> tag specified for this action.检查action中文件下载路径是否正确.");   
				}
			}catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (RowsExceededException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NullPointerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (WriteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				if(null!=in){
					try {
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		return in;
	}

	public void setExcelStream(InputStream excelStream) {
		this.excelStream = excelStream;
	}

	public String getFileNames() {
		return fileNames;
	}

	public void setFileNames(String fileNames) {
		this.fileNames = fileNames;
	}
	
}
